//
//  BLCollectionViewController.m
//  ITC_iOS
//
//  Created by iBenz on 2018/8/25.
//  Copyright © 2018年 Benz.L. All rights reserved.
//

#import "BLCollectionViewController.h"
#import "UIScrollView+RefreshView.h"

@interface BLCollectionViewController () {
    UICollectionView *_collectionView;
}
@end

@implementation BLCollectionViewController

- (instancetype)init {
    self = [super init];
    if (self) {
//        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:UICollectionViewLayout.new];
//        _collectionView.delegate = self;
//        _collectionView.dataSource = self;
//        [self.view addSubview:_collectionView];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.collectionView.frame = self.view.bounds;
    
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.collectionView.frame = self.view.bounds;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return nil;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (void)setRefresh:(PullRefresh)refresh {
    _refresh = refresh;
    WeakRef(self);
    if (refresh&PullRefreshDown) {
        [_collectionView setPullDownHandler:^(UIScrollView *scrollView) {
            [weakself pulldownToRefresh];
        }];
    }
    else {
        [_collectionView setPullDownHandler:nil];
    }
    if (refresh&PullRefreshUp) {
        [_collectionView setPullUpHandler:^(UIScrollView *scrollView) {
            [weakself pullupToRefresh];
        }];
    }
    else {
        [_collectionView setPullUpHandler:nil];
    }
}

- (void)pulldownToRefresh {
    
}
- (void)pullupToRefresh {
    
}
- (void)autoRefresh {
    [_collectionView autoPulldown];
}
- (void)endRefrshWithNomoreData:(BOOL)nomare title:(NSString *)title {
    [_collectionView setNomoreTitle:title];
    [_collectionView endPulllAndMarkAsNomoreData:nomare];
}
- (void)resetNomoreData {
    [_collectionView endPulllAndMarkAsNomoreData:NO];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionFootersPinToVisibleBounds = YES;
        flowLayout.itemSize = (CGSize){10, 10};
        flowLayout.minimumLineSpacing = 1.0;
        flowLayout.minimumInteritemSpacing = 1.0;
        flowLayout.sectionInset = UIEdgeInsetsZero;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = UIColor.whiteColor;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

@end
